import CryptoJS from 'crypto-js';

// 密钥 24 位
const secretKey = CryptoJS.enc.Utf8.parse('wds^7#9%=jwdf%76==9i87tf');
// 初始向量 initial vector 8 位
const iv = CryptoJS.enc.Utf8.parse('01234567');

const key_Int = new Int8Array([32,87,47,82,54,75,63,71,48,80,65,88,17,99,45,43]);
const keyBytes = Int8parse(key_Int); // 数据解析

function Int8parse(u8arr) {
  // Shortcut
  const len = u8arr.length;

  // Convert
  const words = [];
  for (let i = 0; i < len; i++) {
    words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
  }

  return CryptoJS.lib.WordArray.create(words, len);
}

const key = CryptoJS.enc.Utf8.parse("8NONwyJtHesysWpM");

// 4726052c-3ad8-4172-a14c-06eeb110620a727


const key1 = CryptoJS.enc.Utf8.parse('wds^7#9%=jwdf%76==9i87tf');


// 结果

class Crypto {

  static AESencrypt (text) {
    const encrypted = CryptoJS.AES.encrypt(text, key1, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});
    // return encrypted.toString();
    return encrypted.ciphertext.toString();
  }

  static AESdecrypt (text) {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(text);
    const encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decrypt = CryptoJS.AES.decrypt(encryptedBase64Str, key1, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.NoPadding
    });
    return decrypt.toString(CryptoJS.enc.Utf8);
  }

  // Triple DES 加密
  static encrypt (plainText) {
    let encrypted = CryptoJS.TripleDES.encrypt(plainText, secretKey, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    // 转换为字符串
    encrypted = encrypted.toString();
    console.log(encrypted);
    return encrypted;
  }

  // Triple DES 解密
  static decrypt (encryptText) {
    let decrypted = CryptoJS.TripleDES.decrypt(encryptText, secretKey, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    decrypted = decrypted.toString(CryptoJS.enc.Utf8);
    console.log(decrypted);
    return decrypted;
  }

}
export default Crypto;
